#include "trackView.h"


int initHeadTrack()
{
	pData = &data;


	// Load DLL file
	hinstLib = LoadLibrary("FreeTrackClient.dll");
	if (hinstLib == NULL) {
		printf("ERROR: unable to load DLL\n");
		return 1;
	}
	else
	{
		printf("dll loaded\n");
	}

	// Get function pointers
	getData = (importGetData)GetProcAddress(hinstLib, "FTGetData");
	getDllVersion = (importGetDllVersion)GetProcAddress(hinstLib, "FTGetDllVersion");
	reportID = (importReportID)GetProcAddress(hinstLib, "FTReportName");
	provider = (importProvider)GetProcAddress(hinstLib, "FTProvider");

	// Check they are valid
	if (getData == NULL) {
		printf("ERROR: unable to find 'FTGetData' function\n");
		FreeLibrary(hinstLib);
		return 1;
	}
	if (getDllVersion == NULL){
		printf("ERROR: unable to find 'FTGetDllVersion' function\n");
		FreeLibrary(hinstLib);
		return 1;
	}
	if (reportID == NULL){
		printf("ERROR: unable to find 'FTReportID' function\n");
		FreeLibrary(hinstLib);
		return 1;
	}
	if (reportID == NULL){
		printf("ERROR: unable to find 'FTProvider' function\n");
		FreeLibrary(hinstLib);
		return 1;
	}

	//    Print the address of each function
	printf("FTGetData is at address: 0x%x\n",getData);
	printf("FTGetDllVersion is at address: 0x%x\n",getDllVersion);
	printf("FTReportID is at address: 0x%x\n",reportID);
	printf("FTProvider is at address: 0x%x\n",provider);

	//    Call each function and display result
	pDllVersion = getDllVersion();
	printf("Dll Version: %s\n", pDllVersion);

	pProvider = provider();
	printf("Provider: %s\n", pProvider);

	reportID(name);    //not sure what this does - I guess it tells the dll that I am using it.
	
	return 0;
}

int killHeadTrack()
{
	// Unload DLL file
	FreeLibrary(hinstLib);

	return 0;
}

FreeTrackData getHeadTrackData()
{
	updateTracker();
	return data;
}

void updateTracker()
{
	//system("cls"); //clear screen
    if (getData(pData))
    {
        //printf("Record ID: %d\n" , data.dataID);
        //printf("Yaw: %5.2f\n" , data.yaw * 57.3);
        //printf("Pitch: %5.2f\n" , data.pitch * 57.3);
        //printf("Roll: %5.2f\n" , data.roll * 57.3);
        //printf("X: %5.2f\n" , data.x );
        //printf("Y: %5.2f\n" , data.y );
        //printf("Z: %5.2f\n" , data.z );
    }
    else
    {
        printf("Nothing returned from getData\n");
        //break;
    }
}